//
//  BuyAiCountItemView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/5/8.
//

import SwiftUI

struct BuyAiCountItemView2: View {
    @State var bean: AiCountConfigActBean
    let isSelected: Bool
    var body: some View {
        VStack(alignment: .leading,spacing: 0){
            HStack {
                HStack(alignment: .bottom, spacing: 0) {
                    Text(bean.day?.stringValue ?? "0")
                        .font(.system(size: 16))
                        .foregroundStyle(themeManager.themeColor)
                    Text("天")
                        .font(.system(size: 8))
                        .foregroundStyle(themeManager.themeColor)
                        .padding(.bottom,3)
                }.frame(width: 50,height: 50)
                    .background(themeManager.themeColor.opacity(0.2))
                    .clipShape(Circle())
                VStack(alignment:.leading) {
                    Text(bean.title ?? "")
                        .font(.system(size: 14))
                        .foregroundStyle(.bgNormalText)
                    Text(bean.subTitle ?? "")
                        .font(.system(size: 12))
                        .foregroundStyle(.bgNormalText3)
                }
                Spacer()
                HStack(alignment:.bottom,spacing: 0){
                    Text("¥")
                        .font(.system(size: 14))
                        .foregroundStyle(themeManager.themeColor)
                        .padding(.vertical,4)
                    Text(getPice())
                        .font(.system(size: 28))
                        .foregroundStyle(themeManager.themeColor)
                    Text(".")
                        .font(.system(size: 14))
                        .foregroundStyle(themeManager.themeColor)
                        .padding(.vertical,3)
                    Text(getPice2())
                        .font(.system(size: 14))
                        .foregroundStyle(themeManager.themeColor)
                        .padding(.vertical,3)
                }
            }
            if !bean.enabled() {
                Text("库存已抢空！请明天再来")
                    .font(.system(size: 12))
                    .foregroundStyle(.bgNormalText4)
                    .padding(.vertical,2)
            } else {
                Color.clear.frame(height: 15)
            }
        }.frame(maxWidth: .infinity, alignment: .leading)
            .padding(.horizontal,10)
            .padding(.top, 15)
            .background(bean.enabled() ? (isSelected ? themeManager.themeColor.opacity(0.15) : .bgItem) : .disabled)
            .clipShape(RoundedRectangle(cornerRadius: 8))
            .padding(.horizontal,10)
    }
    
    func getPice() -> String {
        let parts = String(bean.price ?? 0.0).components(separatedBy: ".")
        if parts.count == 2 {
            let integerPart = parts[0]  // "3"
            return integerPart
        }
        return bean.price?.stringValue ?? "0"

    }
    
    func getPice2() -> String {
        let parts = String(bean.price ?? 0.0).components(separatedBy: ".")
        if parts.count == 2 {
            let fractionalPart = parts[1] // "14"
            return fractionalPart
        }
        return bean.price?.stringValue ?? "0"

    }
}

#Preview {
    let bean = AiCountConfigActBean()
    bean.title = "我是标题"
    bean.subTitle = "我是副标题我是副标题我是副标题我是副标题我是副标题我是副标题我是副标题我是副标题"
    bean.price = 92.3
    return BuyAiCountItemView2(bean: bean,isSelected: true)
}
